/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.project.web.approvalrecord;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.project.entity.approvalrecord.ApprovalRecord;
import com.idenger.modules.project.service.approvalrecord.ApprovalRecordService;

/**
 * 项目审批Controller
 * @author chenlin
 * @version 2018-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/approvalrecord/approvalRecord")
public class ApprovalRecordController extends BaseController {

	@Autowired
	private ApprovalRecordService approvalRecordService;
	
	@ModelAttribute
	public ApprovalRecord get(@RequestParam(required=false) String id) {
		ApprovalRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = approvalRecordService.get(id);
		}
		if (entity == null){
			entity = new ApprovalRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("project:approvalrecord:approvalRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(ApprovalRecord approvalRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ApprovalRecord> page = approvalRecordService.findPage(new Page<ApprovalRecord>(request, response), approvalRecord); 
		model.addAttribute("page", page);
		return "modules/project/approvalrecord/approvalRecordList";
	}

	@RequiresPermissions("project:approvalrecord:approvalRecord:view")
	@RequestMapping(value = "form")
	public String form(ApprovalRecord approvalRecord, Model model) {
		model.addAttribute("approvalRecord", approvalRecord);
		return "modules/project/approvalrecord/approvalRecordForm";
	}

	@RequiresPermissions("project:approvalrecord:approvalRecord:edit")
	@RequestMapping(value = "save")
	public String save(ApprovalRecord approvalRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, approvalRecord)){
			return form(approvalRecord, model);
		}
		approvalRecordService.save(approvalRecord);
		addMessage(redirectAttributes, "保存项目审批成功");
		return "redirect:"+Global.getAdminPath()+"/project/approvalrecord/approvalRecord/?repage";
	}
	
	@RequiresPermissions("project:approvalrecord:approvalRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(ApprovalRecord approvalRecord, RedirectAttributes redirectAttributes) {
		approvalRecordService.delete(approvalRecord);
		addMessage(redirectAttributes, "删除项目审批成功");
		return "redirect:"+Global.getAdminPath()+"/project/approvalrecord/approvalRecord/?repage";
	}

}